﻿using System;
using System.Collections.Generic;
using System.Text;

namespace PlugIns
{
    using Core;
    using System.Windows.Forms;
    using Microsoft.Office.Core;
    using Microsoft.Office.Interop.Excel;

    public class EPlug : _BasePlugin
    {
        private const string Name = "Выгрузка данных в Excel";
        private const string Info = "PlugIn для выгрузки данных рейтинга в\nMicrosoft Office Excel 2003.\nАвтор: Артамонов Юрий\nВерсия: 1.0.2";
        public string PlugName
        {
            get { return Name; }
        }
        public string PlugInfo
        {
            get { return Info; }
        }
        public void MainMethod(SchoolClass Class, Form owner)
        {
            
            _Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
            Worksheet ws = (Worksheet)wb.Worksheets[1];
            ws.Name = Class.ClassTitle;
            Range point = (Range)ws.Cells[1, 1];
            point.Value2 = "Отчёт";
            point.Font.Size = 18;
            point = (Range)ws.Cells[2, 1];
            point.Value2 = Class.ClassTitle;
            point.Font.Size = 18;
            point = (Range)ws.Cells[3, 1];
            point.Value2 = DateTime.Now.ToShortDateString();
            point.Font.Size = 18;
            point = (Range)ws.Cells[5, 1];
            point.Value2 = "Участник";
            point.Font.Bold = true;
            int x = 0;
            int y = 0;
            #region формирование заголовка таблицы
            for (int i = 0; i < Class.Study.Length; i++)
            {
                //
                x = 3 * (i + 1) - 1;
                y = 4;
                calc(ref x, ref y);
                point = (Range)ws.Cells[x, y];
                point.Value2 = Class.Study[i];
                point.Font.Bold = true;
                //ball
                x = 3 * (i + 1) - 2;
                y = 5;
                calc(ref x, ref y);
                point = (Range)ws.Cells[x, y];
                point.Value2 = "Балл";
                point.Font.Bold = true;
                //place
                x = 3 * (i + 1) - 1;
                y = 5;
                calc(ref x, ref y);
                point = (Range)ws.Cells[x, y];
                point.Value2 = "Место";
                point.Font.Bold = true;
                //bonus
                x = 3 * (i + 1);
                y = 5;
                calc(ref x, ref y);
                point = (Range)ws.Cells[x, y];
                point.Value2 = "Бонус";
                point.Font.Bold = true;
            }
            //global ball
            x = 1 + Class.Study.Length*3;
            y = 5;
            calc(ref x, ref y);
            point = (Range)ws.Cells[x, y];
            point.Value2 = "Всего";
            point.Font.Bold = true;
            //global place
            x = 2 + Class.Study.Length * 3;
            y = 5;
            calc(ref x, ref y);
            point = (Range)ws.Cells[x, y];
            point.Value2 = "Место";
            point.Font.Bold = true;

            #endregion
            #region выгрузка данных
            for (int i = 0; i < Class.group.Length; i++)
            {
                x = 0;
                y = i + 6;
                calc(ref x, ref y);
                point = (Range)ws.Cells[x, y];
                point.Value2 = Class.group[i].Name;
                point.Font.Bold = true;
                for (int j = 0; j < Class.Study.Length; j++)
                {
                    //ball
                    x = 3 * (j + 1) - 2;
                    y = i+6;
                    calc(ref x, ref y);
                    point = (Range)ws.Cells[x, y];
                    point.Value2 = Class.group[i].StudyBall[j];
                    //place
                    x = 3 * (j + 1) - 1;
                    y = i+6;
                    calc(ref x, ref y);
                    point = (Range)ws.Cells[x, y];
                    point.Value2 = Class.group[i].PupilResult[j].Place;
                    //bonus
                    x = 3 * (j + 1);
                    y = i+6;
                    calc(ref x, ref y);
                    point = (Range)ws.Cells[x, y];
                    point.Value2 = Class.group[i].PupilResult[j].Bonus;
                }
                //global ball
                x = 1 + Class.Study.Length * 3;
                y = i + 6;
                calc(ref x, ref y);
                point = (Range)ws.Cells[x, y];
                point.Value2 = Class.group[i].ClassBonus;
                //global place
                x = 2 + Class.Study.Length * 3;
                y = i + 6;
                calc(ref x, ref y);
                point = (Range)ws.Cells[x, y];
                point.Value2 = Class.group[i].ClassPlace;
            }
            #endregion*/
            xlApp.Visible = true;
        }
        private void calc(ref int x, ref  int y)
        {
            int c = x + 1;
            x = y + 1;
            y = c;
        }
    }
}
